/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 */
package com.hyyt.shopjsp.phoneApp.service.imp;

import com.hyyt.shopjsp.phoneApp.dao.PhoneAppHomeCategoryDAO;
import com.hyyt.shopjsp.phoneApp.pojo.PhoneAppHomeCategory;
import com.hyyt.shopjsp.phoneApp.service.IPhoneAppHomeCategoryService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * phone_app_home_category,PHONE_手机APP_端广告分类 Service 实现
 *
 */
@Service
public class PhoneAppHomeCategoryService implements IPhoneAppHomeCategoryService {
    private static  final Logger logger = Logger.getLogger(IPhoneAppHomeCategoryService.class);

    @Resource
    private PhoneAppHomeCategoryDAO phoneAppHomeCategoryDAO;
    @Resource
    private IPhoneAppHomeCategoryService phoneAppHomeCategoryService;

    /**
     * 保存PHONE_手机APP_端广告分类
     *
     * @param phoneAppHomeCategory PHONE_手机APP_端广告分类
     * @return 保存后的PHONE_手机APP_端广告分类
     */
    @Override
    public void savePhoneAppHomeCategory(PhoneAppHomeCategory phoneAppHomeCategory) {
        phoneAppHomeCategory.setIsLeaf(1);
        PhoneAppHomeCategory pahc = phoneAppHomeCategoryService.loadPhoneAppHomeCategory(phoneAppHomeCategory.getParentId());
        //计算级别
        Integer level=0;
        if(pahc!=null){
            level = pahc.getLevelCode();
            pahc.setIsLeaf(0);
            phoneAppHomeCategoryService.updatePhoneAppHomeCategory(pahc);//更改父对象的节点信息
        }
        phoneAppHomeCategory.setLevelCode(level+1);
        phoneAppHomeCategoryDAO.insert(phoneAppHomeCategory);
    }

    /**
     * 更新PHONE_手机APP_端广告分类
     *
     * @param phoneAppHomeCategory PHONE_手机APP_端广告分类
     * @return 更新后的PHONE_手机APP_端广告分类
     */
    @Override
    public void updatePhoneAppHomeCategory(PhoneAppHomeCategory phoneAppHomeCategory) {
        phoneAppHomeCategoryDAO.update(phoneAppHomeCategory);
    }

    /**
     * 删除指定ID的PHONE_手机APP_端广告分类
     *
     * @param id PHONE_手机APP_端广告分类ID
     */
    @Override
    public void deletePhoneAppHomeCategory(Integer id) {
        phoneAppHomeCategoryDAO.delete(id);
    }

    /**
     * 删除指定ID集合的PHONE_手机APP_端广告分类
     *
     * @param ids PHONE_手机APP_端广告分类ID集合
    */
    @Override
    public void deletePhoneAppHomeCategory(Collection<Integer> ids){
        phoneAppHomeCategoryDAO.batchDelete(ids);
    }

    /**
     * 根据ID加载一个PHONE_手机APP_端广告分类
     *
     * @param id PHONE_手机APP_端广告分类ID
     * @return PHONE_手机APP_端广告分类
     */
    @Override
    public PhoneAppHomeCategory loadPhoneAppHomeCategory(Integer id) {
        return phoneAppHomeCategoryDAO.load(id);
    }

    /**
    * 查询所有的PHONE_手机APP_端广告分类，通常有最多取30000条记录的限制
    *
    * @return 所有的PHONE_手机APP_端广告分类，通常有最多取30000条记录的限制
    */
    @Override
    public List<PhoneAppHomeCategory> queryAllPhoneAppHomeCategory() {
        return phoneAppHomeCategoryDAO.queryAll();
    }

    /**
     * 查询PHONE_手机APP_端广告分类
     *
     * @param params 查询参数，为key-value形式
     * @return PHONE_手机APP_端广告分类集合
     */
    @Override
    public List<PhoneAppHomeCategory> queryPhoneAppHomeCategory(Map<String, Object> params) {
        return phoneAppHomeCategoryDAO.query(params);
    }

    /**
     * 查询PHONE_手机APP_端广告分类
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return PHONE_手机APP_端广告分类集合
     */
    @Override
    public List<PhoneAppHomeCategory> queryPhoneAppHomeCategory(Map<String, Object> params, int startRow, int rowSize) {
        return phoneAppHomeCategoryDAO.query(params, startRow, rowSize);
    }

    /**
     * 统计PHONE_手机APP_端广告分类
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countPhoneAppHomeCategory(Map<String, Object> params) {
        return phoneAppHomeCategoryDAO.count(params);
    }
}